/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.gis.eemc.dao;

import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Hibernate;

import java.util.List;

import net.gis.eemc.domain.HibernateUtil;
import net.gis.eemc.domain.Speciality;
import net.gis.eemc.domain.Department;
import net.gis.eemc.domain.Faculty;

import net.gis.eemc.dao.DepartmentDAO;
import net.gis.eemc.dao.DepartmentDAOImpl;

/**
 *
 * @author Sergey
 */
public class SpecialityDAOImpl implements SpecialityDAO {

    Session session = null;

    @Override
    public void addSpeciality(Speciality speciality) {
        org.hibernate.Transaction tx = null;

        try {
            this.session = HibernateUtil.getSessionFactory().getCurrentSession();

            tx = session.beginTransaction();
            session.save(speciality);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
    }

    @Override
    public List<Speciality> listSpeciality() {
        return getSpeciality("from Speciality as speciality");
    }

    @Override
    public List<Speciality> listSpeciality(Integer dep_id) {
        return getSpeciality("from Speciality as speciality where speciality.department.idDep = \"" + dep_id.toString() + "\"");
    }

    @Override
    public List<Speciality> listSpeciality(Department dep) {
        return listSpeciality(dep.getIdDep());
    }

    @Override
    public List<Speciality> listSpeciality(Faculty fac) {
        org.hibernate.Transaction tx = null;
        List<Speciality> specialities = new ArrayList<Speciality>();

        try {
            this.session = HibernateUtil.getSessionFactory().getCurrentSession();
            /*Hibernate.initialize(fac);
            Hibernate.initialize(fac.getDepartments());*/
            
            tx = session.beginTransaction();
            session.refresh(fac);
            for (Department dep : fac.getDepartments()) {
                specialities.addAll(listSpeciality(dep));
            }

            //tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }

        return specialities;
    }

    @Override
    public void removeSpeciality(Integer id) {
        org.hibernate.Transaction tx = null;

        try {
            this.session = HibernateUtil.getSessionFactory().getCurrentSession();

            tx = session.beginTransaction();
            String hqlDelete = "delete Speciality spec where spec.idSpec = :id";
            Query q = session.createQuery(hqlDelete);
            q.setString("id", id.toString());
            q.executeUpdate();
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
    }

    @Override
    public void removeSpeciality(Speciality spec) {
        org.hibernate.Transaction tx = null;

        try {
            this.session = HibernateUtil.getSessionFactory().getCurrentSession();

            tx = session.beginTransaction();
            session.delete(spec);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
    }

    @Override
    public void updateSpeciality(Speciality spec) {
        org.hibernate.Transaction tx = null;

        try {
            this.session = HibernateUtil.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            session.update(spec);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
    }

    @Override
    public List<Speciality> getSpecialityWithTitle(String Title) {
        return getSpeciality("from Speciality as speciality where speciality.title = \"" + Title + "\"");
    }

    @Override
    public List<Speciality> getSpecialityWithShortName(String shortName) {
        return getSpeciality("from Speciality as speciality where speciality.shortName = \"" + shortName + "\"");
    }

    @Override
    public List<Speciality> getSpecialityWithRefNumber(String refNumber) {
        return getSpeciality("from Speciality as speciality where speciality.refNum = \"" + refNumber + "\"");
    }

    @Override
    public Speciality getSpecialityWithID(Integer id) {
        return getSpeciality("from Speciality as speciality where speciality.idSpec = \"" + id.toString() + "\"").get(0);
    }

    private List<Speciality> getSpeciality(String query) {
        List<Speciality> specList = null;
        org.hibernate.Transaction tx = null;

        try {
            this.session = HibernateUtil.getSessionFactory().getCurrentSession();

            tx = session.beginTransaction();
            Query q = session.createQuery(query);
            specList = (List<Speciality>) q.list();
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
        return specList;
    }
}
